package com.piotrs10.bsk.gui.views;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Iterator;

import com.piotrs10.bsk.dao.KlientDao;
import com.piotrs10.bsk.dao.SamochodDao;
import com.piotrs10.bsk.dao.SprzedazDao;
import com.piotrs10.bsk.model.Klient;
import com.piotrs10.bsk.model.Samochod;
import com.piotrs10.bsk.model.Sprzedaz;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.PopupDateField;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Window;

public class ModifySprzedaz extends ModifyLayout {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8761132450877603459L;

	private TextField sumaField, iloscField;
	private ComboBox klientBox;
	private PopupDateField dataField;

	public ModifySprzedaz(boolean isInsert, Integer id, Window modalWindow) {
		super(isInsert, modalWindow);

		this.sumaField = new TextField("Suma");
		this.iloscField = new TextField("Ilosc");
		this.klientBox = new ComboBox("Klient");
		this.dataField = new PopupDateField("Data");
		this.dataField.setValue(new java.util.Date());
		this.dataField.setResolution(PopupDateField.RESOLUTION_DAY);
		this.loadCustomers(isInsert, id);

		this.fieldsPanel.addComponent(this.sumaField);
		this.fieldsPanel.addComponent(this.iloscField);
		this.fieldsPanel.addComponent(this.klientBox);
		this.fieldsPanel.addComponent(this.dataField);
		this.fieldsPanel.addComponent(this.commitButton);
	}

	private void loadCustomers(boolean insert, Integer id) {
		// TODO: Pobrac z bazy danych co trzeba
		ArrayList<Klient> list = new KlientDao().getAll();
		for (Klient current : list) {
			this.klientBox.addItem(current.getId());
		}
		
		if(insert == false) {
			Sprzedaz s = new SprzedazDao().get(id);
			this.sumaField.setValue(s.getSuma());
			this.iloscField.setValue(s.getIlosc());
			this.dataField.setValue(s.getData());
			
			Iterator<?> iterator = this.klientBox.getItemIds().iterator();
			Object item = null;
			int typId = 0;
			do {
				item = iterator.next();
				typId = Integer.valueOf(this.klientBox.getItemCaption(item));
			} while(typId != s.getKlient_id());
			this.klientBox.setValue(item);
		}
	}

	@Override
	public void buttonClick(ClickEvent event) {
		// TODO Auto-generated method stub

	}

	public static void fillTable(Table table) {
		table.addContainerProperty("ID", Integer.class, null);
		table.addContainerProperty("Suma", Float.class, null);
		table.addContainerProperty("Ilosc", Integer.class, null);
		table.addContainerProperty("Klient", Integer.class, null);
		table.addContainerProperty("Data", Date.class, null);
		ArrayList<Sprzedaz> list = new SprzedazDao().getAll();
		for (int j = 0; j < list.size(); j++) {
			Sprzedaz s = list.get(j);
			table.addItem(new Object[] { s.getId(), s.getSuma(), s.getIlosc(),
					s.getKlient_id(), s.getData() }, j);
		}
	}
	
	public static void refresh(Table table) {
		table.removeAllItems();
		ArrayList<Sprzedaz> list = new SprzedazDao().getAll();
		for (int j = 0; j < list.size(); j++) {
			Sprzedaz s = list.get(j);
			table.addItem(new Object[] { s.getId(), s.getSuma(), s.getIlosc(),
					s.getKlient_id(), s.getData() }, j);
		}
	}

	public String getWinWidth() {
		return "250px";
	}

	public String getWinHeight() {
		return "260px";
	}
}
